package service;

import beanInfo.MeaningInfo;
import beanInfo.WordInfo;
import beans.Meaning;
import beans.User;
import beans.Word;
import dao.MeaningDAO;
import dao.UserDAO;
import dao.WordDAO;
import db.dictionaryTrie.DictionaryTrie;
import model.ApiResultModel;
import model.SearchResultModel;

import java.util.ArrayList;
import java.util.List;

public class DictionaryService {

    public static ApiResultModel add(long user_id, int add_mode, String word_string, String meanings) {
        User user = UserDAO.ME.getById(user_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("用户未登录！");
        }
        word_string = word_string.trim();
        if (!WordInfo.isLengthLegal(word_string)) {
            return ApiResultModel.failedWithMessage("单词长度超出限制！");
        }
        Word word = WordDAO.ME.getByWordString(word_string);
        if (word == null) {
            word = new Word();
            word.setCreate_user(user_id);
            word.setWord_string(word_string);
            long wordId = word.SaveToDB();
            if (wordId <= 0) {
                return ApiResultModel.failedWithMessage("数据插入失败！");
            }
            //在字典树中插入单词
            DictionaryTrie.insert(word.getWord_string());
            user.setWord_count(user.getWord_count() + 1);
            word.setId(wordId);
        } else if (add_mode == MeaningInfo.ADD_WORD) {
            return ApiResultModel.failedWithMessage("单词" + word_string + "已存在！");
        }
        String[] meaningArray = meanings.split(MeaningInfo.SEPARATOR);
        int count = 0;
        for (String meaningStr : meaningArray) {
            //去重查询太耗时了，因此不做去重的设计
            meaningStr = meaningStr.trim();
            if (MeaningInfo.isLengthLegal(meaningStr)) {
                Meaning meaning = new Meaning();
                meaning.setWord_id(word.getId());
                meaning.setCreate_user(user_id);
                meaning.setWord_meaning(meaningStr);
                meaning.SaveToDB();
                count++;
            }
        }
        user.setMeaning_count(user.getMeaning_count() + count);
        //更新用户单词、单词释义贡献数
        user.DoUpdate();
        return ApiResultModel.successWithObject(word);
    }

    public static ApiResultModel deleteWord(long user_id, long word_id) {
        User user = UserDAO.ME.getById(user_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("用户未登录！");
        }
        Word word = WordDAO.ME.getById(word_id);
        if (word != null) {
            long wordDeleteResult = word.Delete();
            if (wordDeleteResult <= 0) {
                return ApiResultModel.failedWithMessage("删除失败！");
            }
            //删除字典树中的单词
            DictionaryTrie.delete(word.getWord_string());
            //删除一个单词关联的所有单词含义
            MeaningDAO.ME.deleteWordMeanings(word_id);
        }
        return ApiResultModel.successWithMessage("删除成功！");
    }

    public static ApiResultModel deleteMeaning(long user_id, long meaning_id) {
        Meaning meaning = MeaningDAO.ME.getById(meaning_id);
        if (meaning != null) {
            long deleteResult = meaning.Delete();
            if (deleteResult <= 0) {
                return ApiResultModel.failedWithMessage("删除失败！");
            }
        }
        return ApiResultModel.successWithMessage("删除成功！");
    }

    public static ApiResultModel searchWord(long word_id, String wordStr) {
        Word word = WordDAO.ME.getById(word_id);
        if (word == null) {
            word = WordDAO.ME.getByWordString(wordStr);
            if (word == null)
            return ApiResultModel.failedWithMessage("字典中暂无所查找的单词");
        }
        List<Meaning> wordMeaningList = MeaningDAO.ME.listWordMeanings(word.getId());
        SearchResultModel searchResultModel = new SearchResultModel(word, wordMeaningList);
        return ApiResultModel.successWithObject(searchResultModel);
    }

    public static ApiResultModel getNewWords(int num) {
        //最多获取100条
        if (num > 100) num = 100;
        List<Word> newWordList = WordDAO.ME.listNewWords(num);
        return ApiResultModel.successWithObject(newWordList);
    }

    public static ApiResultModel updateMeaning(long user_id, long meaning_id, String update_meaning) {
        User user = UserDAO.ME.getById(user_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("用户未登录！");
        }
        update_meaning = update_meaning.trim();
        if (!MeaningInfo.isLengthLegal(update_meaning)) {
            return ApiResultModel.failedWithMessage("释义长度超出范围");
        }
        Meaning meaning = MeaningDAO.ME.getById(meaning_id);
        if (meaning == null) {
            return ApiResultModel.failedWithMessage("此释义不存在或已被删除");
        }
        meaning.setWord_meaning(update_meaning);
        meaning.setUpdate_user(user_id);
        long result = meaning.DoUpdate();
        if (result > 0) {
            user.setUpdate_meaning_count(user.getUpdate_meaning_count() + 1);
            user.DoUpdate();
            return ApiResultModel.successWithMessage("修改成功");
        }
        return ApiResultModel.failedWithMessage("修改失败");
    }

    public static ApiResultModel getSearchHint(String prefix, int num) {
        List<String> searchHintList = new ArrayList<>();
        if (!prefix.isEmpty() && prefix.length() <= WordInfo.WORD_MAX_LENGTH) {
            searchHintList = DictionaryTrie.prefixSearch(prefix, num);
        }
        return ApiResultModel.successWithObject(searchHintList);
    }

    public static ApiResultModel getRandomWord() {
        Word word = WordDAO.ME.getRandomWord();
        List<Meaning> wordMeaningList = MeaningDAO.ME.listWordMeanings(word.getId());
        SearchResultModel searchResultModel = new SearchResultModel(word, wordMeaningList);
        return ApiResultModel.successWithObject(searchResultModel);
    }
}
